package edu.cuc.chap02.lianxi.l3.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

public class TimeServerHandlerL3 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().id());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf b = (ByteBuf) msg;
        byte[] bytes = new byte[b.readableBytes()];
        b.readBytes(bytes);
        b.clear();
        String string = new String(bytes);
        System.out.println(string);
        String resp = "QUERY TIME ORDER".equalsIgnoreCase(string) ? new Date().getTime() + "" : "BAD ORDER";
        ctx.writeAndFlush(b.writeBytes(resp.getBytes()));
    }
}
